<?php

/*
 * 这是公共layout类
 */

class Layout {

    var $obj;
    var $layout;

    function __construct($params = array("type" => "default")) {
        $this->obj = & get_instance();
        $this->layout = $params["type"] . "/layout";
        $this->mJsCss = $params["type"] . "/pageJsCss";
        $this->mHead = $params["type"] . "/pageHead";
    }

    function view($view, $data = null, $retrun = false) {
        $data['headTitle'] = empty($data["headTitle"]) ? WEB_NAME : $data["headTitle"];
        $data['headKeywords'] = empty($data["headKeywords"]) ? '' : $data["headKeywords"];
        $data['headDescription'] = empty($data["headDescription"]) ? '这里是' . WEB_NAME . '!' : $data["headDescription"];

        $data['template']["head"] = $this->getHead($data);
        $data['template']["jsCss"] = $this->getJsCss($data);

        $data['layoutContent'] = $this->getLayoutContent($view, $data);
        $data["pageName"] = empty($data["pageName"]) ? $this->obj->router->fetch_directory() . $this->obj->router->fetch_class() . $this->obj->router->fetch_method() : $data["pageName"];

        $this->obj->load->view($this->layout, $data, false);
    }

    /*
     * 得到布局主内容
     */

    function getLayoutContent($view, $data = null) {

        $cssFile = array();
        if (!empty($data["cssFile"])) {
            $cssFile = array_merge($cssFile, $data["cssFile"]);
        }
        $jsFile = array();
        if (!empty($data["jsFile"])) {
            $jsFile = array_merge($jsFile, $data["jsFile"]);
        }

        $data["cssFile"] = $cssFile;
        $data["jsFile"] = $jsFile;

        return $this->obj->load->view($view, $data, true);
    }

    /*
     * 得到head文件
     */

    function getHead($data) {
        return $this->obj->load->view($this->mHead, $data, true);
    }

    /*
     * 得到JSCSS文件
     */

    function getJsCss($data) {
        return $this->obj->load->view($this->mJsCss, $data, true);
    }

}
